import {
  MigrationInterface,
  QueryRunner,
  Table,
  TableForeignKey,
} from 'typeorm';

export class CreateCommentsTable1730623000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建评论表
    await queryRunner.createTable(
      new Table({
        name: 'comments',
        columns: [
          {
            name: 'id',
            type: 'int',
            isPrimary: true,
            isGenerated: true,
            generationStrategy: 'increment',
          },
          {
            name: 'workId',
            type: 'int',
            comment: '作品ID',
          },
          {
            name: 'userId',
            type: 'int',
            comment: '评论者ID',
          },
          {
            name: 'content',
            type: 'text',
            comment: '评论内容',
          },
          {
            name: 'parentId',
            type: 'int',
            isNullable: true,
            comment: '父评论ID（一级评论为null，二级评论有值）',
          },
          {
            name: 'replyToUserId',
            type: 'int',
            isNullable: true,
            comment: '回复的用户ID',
          },
          {
            name: 'likesCount',
            type: 'int',
            default: 0,
            comment: '点赞数',
          },
          {
            name: 'repliesCount',
            type: 'int',
            default: 0,
            comment: '回复数',
          },
          {
            name: 'isDeleted',
            type: 'boolean',
            default: false,
            comment: '是否被删除',
          },
          {
            name: 'createdAt',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
            comment: '创建时间',
          },
          {
            name: 'updatedAt',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
            onUpdate: 'CURRENT_TIMESTAMP',
            comment: '更新时间',
          },
        ],
      }),
      true,
    );

    // 添加外键
    await queryRunner.createForeignKey(
      'comments',
      new TableForeignKey({
        columnNames: ['userId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'users',
        onDelete: 'CASCADE',
      }),
    );

    await queryRunner.createForeignKey(
      'comments',
      new TableForeignKey({
        columnNames: ['workId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'works',
        onDelete: 'CASCADE',
      }),
    );

    await queryRunner.createForeignKey(
      'comments',
      new TableForeignKey({
        columnNames: ['parentId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'comments',
        onDelete: 'CASCADE',
      }),
    );

    await queryRunner.createForeignKey(
      'comments',
      new TableForeignKey({
        columnNames: ['replyToUserId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'users',
        onDelete: 'SET NULL',
      }),
    );

    // 创建索引
    await queryRunner.query(
      'CREATE INDEX `IDX_comments_workId` ON `comments` (`workId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_comments_userId` ON `comments` (`userId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_comments_parentId` ON `comments` (`parentId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_comments_createdAt` ON `comments` (`createdAt`)',
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // 删除索引
    await queryRunner.query(
      'DROP INDEX `IDX_comments_createdAt` ON `comments`',
    );
    await queryRunner.query('DROP INDEX `IDX_comments_parentId` ON `comments`');
    await queryRunner.query('DROP INDEX `IDX_comments_userId` ON `comments`');
    await queryRunner.query('DROP INDEX `IDX_comments_workId` ON `comments`');

    // 删除表
    await queryRunner.dropTable('comments');
  }
}
